const cors = require('cors');
const path = require('path');
const jwt = require('express-jwt');

const express = require('express');
const app = express();
app.listen(3007,() => {console.log('大事件项目接口服务启动了')});


//-----------------中间件---------------------
app.use(cors());
app.use(express.urlencoded({extended : false}));
app.use(jwt({secret : 'biggerevent'}).unless({path : /^\/api/ }));


//-----------------加载路由模块----------------
app.use('/api', require(path.join(__dirname, 'routers', 'login.js')));
app.use('/my', require(path.join(__dirname, 'routers', 'user.js')));
app.use('/my/artical', require(path.join(__dirname, 'routers', 'category.js')));
app.use('/mt/artical', require(path.join(__dirname, 'routers', 'artical.js')));


//-----------------处理错误中间件--------------
app.use(function (err, req, res, next) {
    if (err.name = 'UnauthorizedError') {
        console.log(err.message);
        res.status(401).send({status : 1, message : '身份认证失败！'});
    }
});